/*
Copyright 2010 Dmitry Barashev

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package net.barashev.seclub.corpus.wikipedia;

import java.io.InputStream;
import java.util.Iterator;

import net.barashev.seclub.corpus.Corpus;
import net.barashev.seclub.corpus.Document;
import net.barashev.seclub.corpus.wikipedia.WikipediaExportFileParser.Doc;

public abstract class WikipediaCorpusBase implements Corpus {
    public WikipediaCorpusBase() {
    }

    public Document getDocument(int id) {
        IteratorImpl it = (IteratorImpl) getDocuments();
        for (Document d: it) {
            if (d.getID() == id) {
                it.release();
                return d;
            }
        }
        return null;
    }

    public Iterable<Document> getDocuments() {
        return new IteratorImpl(createInputStream());
    }

    protected abstract InputStream createInputStream();

    private static class IteratorImpl implements Iterable<Document>, Iterator<Document> {
        private WikipediaExportFileParser builder;
        private Object callbackWaiter = new Object();
        private Doc nextDoc;
        private InputStream input;

        public IteratorImpl(InputStream input) {
            this.input = input;
        }

        private void release() {
            this.builder.stop();
        }

        public Iterator<Document> iterator() {
            advance();
            return this;
        }

        public boolean hasNext() {
            return this.nextDoc != null;
        }

        public Document next() {
            Document result = new Document(this.nextDoc.id, this.nextDoc.content, new int[0]);
            advance();
            return result;
        }

        public void remove() {
            throw new UnsupportedOperationException();
        }

        private void advance() {
            synchronized (this.callbackWaiter) {
                WikipediaExportFileParser.Callback callback = new WikipediaExportFileParser.Callback() {
                    public void run(Doc doc) {
                        processDoc(doc);
                    }
                };
                if (this.builder == null) {
                    this.builder = new WikipediaExportFileParser();
                    this.builder.start(this.input, callback);
                } else {
                    builder.getNextDocument(callback);
                }
                try {
                    this.callbackWaiter.wait();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        private void processDoc(Doc doc) {
            synchronized(this.callbackWaiter) {
                this.nextDoc = doc;
                this.callbackWaiter.notifyAll();
                if (doc == null) {
                    release();
                }
            }
        }

        @Override
        protected void finalize() throws Throwable {
            release();
        }
    }
}

